<?php
namespace printing;
use printing\lib\YLYOpenApiClient;
// require_once (__DIR__.'/lib/YLYOpenApiClient.php');
class printDemo{
    public function random ($len) {
        $param = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $str = '';
        for ($i = 0; $i < $len; $i++) {
            $s = substr($param, rand(0, strlen($param) - 1), 1);
            $str .= $s;
        }
        return $str;
    }

    public function payprinting($machineCode,$community_name,$room_address,$owner_name,$detailed,$trade_no,$pay_type,$bill_entry_amount,$gmt_payment,$property){
        $machineCode = $machineCode;//授权的终端号
        $accessToken = '40552d86657c4bdcae446a287ff754f5';//api访问令牌
        $originId = date('YmdHis', time()).$this->random(18);//商户自定义id
        $timesTamp = time();//当前服务器时间戳(10位)

        $content = '';//打印内容
        $content .= '<center>支付宝生活缴费</center>';
        $content .= str_repeat('-',32);
        $content .= '<FH>';
        $content .= '所属小区: '.$community_name."\n";
        $content .= '门牌号: '.$room_address."\n";
        $content .= '业主姓名: '.$owner_name."\n";
        $content .= '支付方式: '.$pay_type."\n";
        $content .= '付款金额: '.$bill_entry_amount."\n";
        $content .= '付款时间: '.$gmt_payment."\n";
        $content .= '收款单位: '.$property."\n";
        $content .= '</FH>';
        $content .= str_repeat('-',16)."\n";
        $content .= '缴费明细: '."\n";
        foreach($detailed as $det){
            $content .= $det."\n";
        }
        $content .= str_repeat('-',32);

        $api = new YLYOpenApiClient();
        $printing = $api->printIndex($machineCode,$accessToken,$content,$originId,$timesTamp);
        return $printing;
        if($printing){
            return '打印成功'; 
        }else{
            return ''; 
        }
    }

    public function cashierprinting($machineCode,$community_name,$company,$business_type,$pay_type,$bill_entry_amount,$gmt_payment,$phone){
        $machineCode = $machineCode;//授权的终端号
        $accessToken = '40552d86657c4bdcae446a287ff754f5';//api访问令牌
        $originId = date('YmdHis', time()).$this->random(18);//商户自定义id
        $timesTamp = time();//当前服务器时间戳(10位)

        $content = '';//打印内容
        $content .= '<center>智慧社区缴费</center>';
        $content .= str_repeat('-',32);
        $content .= '<FH>';
        $content .= '打印订单号: '.$originId."\n";
        $content .= '所属小区: '.$community_name."\n";
        $content .= '业务类型: '.$business_type."\n";
        $content .= '支付方式: '.$pay_type."\n";
        $content .= '付款金额: '.$bill_entry_amount."\n";
        $content .= '付款时间: '.$gmt_payment."\n";
        $content .= '付款单位: '.$company."\n";
        $content .= '联系电话: '.$phone."\n";
        $content .= '</FH>';
        $content .= str_repeat('-',16)."\n";
        
        $content .= str_repeat('-',32);

        $api = new YLYOpenApiClient();
        $printing = $api->printIndex($machineCode,$accessToken,$content,$originId,$timesTamp);
        return $printing;
    }
    
    
    public function gatepayprinting($machineCode,$community_name,$gateName,$pay_type,$bill_entry_amount,$gmt_payment,$remark){
        $machineCode = $machineCode;//授权的终端号
        $accessToken = '40552d86657c4bdcae446a287ff754f5';//api访问令牌
        $originId = date('YmdHis', time()).$this->random(18);//商户自定义id
        $timesTamp = time();//当前服务器时间戳(10位)

        $content = '';//打印内容
        $content .= '<center>智慧社区缴费</center>';
        $content .= str_repeat('-',32);
        $content .= '<FH>';
        $content .= '所属小区: '.$community_name."\n";
        $content .= '所属岗亭: '.$gateName."\n";
        $content .= '支付方式: '.$pay_type."\n";
        $content .= '付款金额: '.$bill_entry_amount."\n";
        $content .= '付款时间: '.$gmt_payment."\n";
        $content .= '备注: '.$remark."\n";
        
        $content .= '</FH>';
        $content .= str_repeat('-',16)."\n";
        
        $content .= str_repeat('-',32);

        $api = new YLYOpenApiClient();
        $printing = $api->printIndex($machineCode,$accessToken,$content,$originId,$timesTamp);
        return $printing;
    }
    public function invoice($machineCode,$community_name,$invoiceurl){
        $machineCode = $machineCode;//授权的终端号
        $accessToken = '40552d86657c4bdcae446a287ff754f5';//api访问令牌
        $originId = date('YmdHis', time()).$this->random(18);//商户自定义id
        $timesTamp = time();//当前服务器时间戳(10位)

        $content = '';//打印内容
        $content .= '<center>智慧社区缴费</center>';
        $content .= str_repeat('-',32);
        $content .= '<FH>';
        $content .= '所属小区: '.$community_name."\n";
        
        
        $content .= '</FH>';
        $content .= '<QR>';
        $content .= $invoiceurl;
        $content .= '</QR>';
        $content .= str_repeat('-',16)."\n";
        
        $content .= str_repeat('-',32);

        $api = new YLYOpenApiClient();
        $printing = $api->printIndex($machineCode,$accessToken,$content,$originId,$timesTamp);
        return $printing;
    }
}